#include "tiledsprite.h"
#include "texturemanager.h"
#include <iostream>


TiledSprite::TiledSprite(const std::string &filename,unsigned nCols, unsigned nRows){

	//Obtiene la textura del manejador
	const sf::Image &img=TextureManager::GetInstance().GetTexture(filename);

	//la asigna al sprite
	SetImage(img);
	

	this->nCols=nCols;
	this->nRows=nRows;
	
	//Calcula dimensiones del sprite
	tileW=img.GetWidth()/nCols;
	tileH=img.GetHeight()/nRows;

	//Inicializa el índice
	tileIndex=-1;


	//El primer tile es el actual
	SetTileIndex(0);

}


void TiledSprite::SetTileIndex(unsigned i){

	//Si no está ya asignado
	if(tileIndex!=i)
	{
		tileIndex=i;
	
		int left, top, right, bottom;
		left=(tileIndex%nCols)*tileW;
		top=(tileIndex/nCols)*tileW;
		right=left+tileW;
		bottom=top+tileH;

		SetSubRect(sf::IntRect(left,top,right,bottom));
	}		


}

unsigned TiledSprite::GetTileIndex(){
	return tileIndex;
}

sf::IntRect TiledSprite::GetAABB(){

	int left, top, right, bottom;
        left=(tileIndex%nCols)*tileW;
        top=(tileIndex/nCols)*tileW;
        right=left+tileW;
        bottom=top+tileH;

        return sf::IntRect(left,top,right,bottom);



}

